package exception

import (
	"fmt"
	"go-web/controllers"
	"net/http"

	"github.com/gin-gonic/gin"
)

func GlobalExceptionHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				// 在这里处理异常
				fmt.Println("捕获异常：", err)
				// 返回自定义的错误响应
				controllers.ReturnError(c, http.StatusInternalServerError, "Internal Server Error")
				// 中止后续处理
				c.Abort()
			}
		}()
		c.Next()
	}
}
